//
//  JJComprehevsiveInfoCell.m
//  HeiheIPad
//
//  Created by tangbin on 2021/3/19.
//  Copyright © 2021 jjkj. All rights reserved.
//

#import "JJComprehevsiveInfoCell.h"
#import "JJMaxOrMinLayer.h"

@interface JJComprehevsiveInfoCell ()

@property (nonatomic, strong) CATextLayer *textLayer;

@property (nonatomic, strong) JJMaxOrMinLayer *maxLayer;
@property (nonatomic, strong) JJMaxOrMinLayer *minLayer;

@property (nonatomic, strong) UILabel *textLabel;

@property (nonatomic, strong) UIImageView *upImgView;

@property (nonatomic, strong) UIButton *btnCell;


@end

@implementation JJComprehevsiveInfoCell

- (void)initWithSubViewCell
{
    [self configUI];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    self.textLabel.frame = self.bounds;
//    self.maxLayer.frame = CGRectMake(self.bounds.size.width/2, (self.bounds.size.height-15)/2, 36, 15);
//    self.minLayer.frame = CGRectMake(self.bounds.size.width/2, (self.bounds.size.height-15)/2, 36, 15);
    
    CGRect frame = CGRectMake(self.bounds.size.width - 15 - 20, (self.bounds.size.height - 20) * 0.5, 20, 20);
    self.upImgView.frame = frame;
    
    self.btnCell.frame = self.bounds;
}


#pragma mark - Private Methods

- (void)configUI
{
    self.contentView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.contentView.layer.borderWidth = 0.5f;
    self.contentView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.textLabel];
//    [self.contentView.layer addSublayer:self.textLayer];
//    [self.contentView.layer insertSublayer:self.maxLayer atIndex:0];
//    [self.contentView.layer insertSublayer:self.minLayer atIndex:0];
    
    [self.contentView addSubview:self.upImgView];
    
    [self.contentView addSubview:self.btnCell];
//
//    [self.maxLayer setNeedsDisplay];
//    [self.minLayer setNeedsDisplay];
    self.contentView.clipsToBounds = YES;
    self.clipsToBounds = YES;
}

#pragma mark - Getter Methods

- (UILabel *)textLabel
{
    if (!_textLabel)
    {
        _textLabel                 = [[UILabel alloc] init];
        _textLabel.textAlignment   = NSTextAlignmentCenter;
        _textLabel.textColor       = [UIColor blackColor];
        _textLabel.lineBreakMode   = NSLineBreakByTruncatingTail;
        _textLabel.font            = [UIFont systemFontOfSize:15];
        _textLabel.numberOfLines   = 0;
//        _textLabel.backgroundColor = [UIColor colorWithRandom];
        
        @weakify(self);
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            @strongify(self);
            if (self.touchViewBlock)
            {
                self.touchViewBlock();
            }
        }];
        [_textLabel addGestureRecognizer:tap];
        _textLabel.userInteractionEnabled = YES;
    }
    return _textLabel;
}

- (UIImageView *)upImgView
{
    if (!_upImgView)
    {
        CGRect frame = CGRectMake(0, 0, 20, 20);
        _upImgView = [[UIImageView alloc] initWithFrame:frame];
        _upImgView.image = [UIImage imageNamed:@"ic_up_no"];
        _upImgView.hidden = YES;
    }
    return _upImgView;
}

- (UIButton *)btnCell
{
    if (!_btnCell)
    {
        _btnCell = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnCell addTarget:self action:@selector(actionClick:) forControlEvents:UIControlEventTouchUpInside];
        _btnCell.hidden = YES;
    }
    return _btnCell;
}

- (void)actionClick:(id)sender
{
    if (self.touchBtnBlock)
    {
        self.touchBtnBlock(self.upImgView);
    }
}


- (CATextLayer *)textLayer
{
    if (!_textLayer) {
        _textLayer = [[CATextLayer alloc] init];
        _textLayer.alignmentMode = kCAAlignmentCenter;
        _textLayer.contentsScale = [UIScreen mainScreen].scale;
        _textLayer.foregroundColor = [UIColor blackColor].CGColor;
        _textLayer.alignmentMode = kCAAlignmentCenter;
        _textLayer.wrapped = NO;
        _textLayer.truncationMode = kCATruncationEnd;
        UIFont *font = [UIFont systemFontOfSize:15];
        CFStringRef fontName = (__bridge CFStringRef)font.fontName;
        CGFontRef fontRef = CGFontCreateWithFontName(fontName);
        _textLayer.font = fontRef;
        _textLayer.fontSize = font.pointSize;
        CGFontRelease(fontRef);
    }
    return _textLayer;
}

- (JJMaxOrMinLayer *)maxLayer
{
    if (!_maxLayer)
    {
        _maxLayer = [JJMaxOrMinLayer layer];
        _maxLayer.type = FCMaxOrMinLayerTypeMax;
        _maxLayer.bounds = CGRectMake(0, 0, 36, 15) ;
        _maxLayer.contentsScale = [UIScreen mainScreen].scale;
        _maxLayer.hidden = YES;
    }
    return _maxLayer;
}
- (JJMaxOrMinLayer *)minLayer
{
    if (!_minLayer)
    {
        _minLayer = [JJMaxOrMinLayer layer];
        _minLayer.type = FCMaxOrMinLayerTypeMin;
        _minLayer.bounds = CGRectMake(0, 0, 36, 15) ;
        _minLayer.contentsScale = [UIScreen mainScreen].scale;
        _minLayer.hidden = YES;
    }
    return _minLayer;
}

#pragma mark - Setter Methods

- (void)setText:(NSString *)text
{
    _text = text;
    self.textLabel.text = text;
}

- (void)setFont:(UIFont *)font
{
    _font = font;
    self.textLabel.font = font;
}

- (void)setTextColor:(UIColor *)textColor
{
    _textColor = textColor;
    self.textLabel.textColor = textColor;
}

- (void)setCellType:(FCChartCollectionViewCellType)cellType
{
    _cellType = cellType;
    switch (cellType)
    {
        case FCChartCollectionViewCellTypeDefault:
        {
            self.textLayer.frame = self.bounds;
            self.minLayer.hidden = YES;
            self.maxLayer.hidden = YES;
            self.textLayer.alignmentMode = kCAAlignmentCenter;
        }
            break;
        case FCChartCollectionViewCellTypeMax:
        {
            self.textLayer.frame = CGRectMake(0, (self.bounds.size.height-21)/2, self.bounds.size.width/2, 16);
            self.minLayer.hidden = YES;
            self.maxLayer.hidden = NO;
            self.textLayer.alignmentMode = kCAAlignmentRight;

        }
            break;
        case FCChartCollectionViewCellTypeMin:
        {
            self.textLayer.frame = CGRectMake(0, (self.bounds.size.height-21)/2, self.bounds.size.width/2, 16);
            self.minLayer.hidden = NO;
            self.maxLayer.hidden = YES;
            self.textLayer.alignmentMode = kCAAlignmentRight;

        }
            break;
            
        default:
            break;
    }
}



@end
